import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.SQLException;

import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.JToolBar;
import javax.swing.SwingConstants;
import javax.swing.WindowConstants;


public class GUI extends JFrame {
        /* size of text fields */
        private static final int FIELD_SIZE = 20;
        /* width of GUI */
        private static final int WIDTH = 500;
        /* height of GUI */
        private static final int HEIGHT = 500;
       
        private EmbroideryDB DB;
       
        private JTextField minPrice;
        private JTextField maxPrice;
        private JTextField minThread;
        private JTextField maxThread;

        public GUI(EmbroideryDB db) {
                super("Embroidery Database");
                DB = db;
        }
       
        /**
         * initializes gui.
         */
        public void start() {
                setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
                setSize(new Dimension(WIDTH,HEIGHT));
                setResizable(false);
               
                if (Toolkit.getDefaultToolkit().isFrameStateSupported(JFrame.MAXIMIZED_BOTH))
                        setExtendedState(getExtendedState() | JFrame.MAXIMIZED_BOTH);
               
                final JToolBar sideBar = sideBar();
                add(sideBar, BorderLayout.WEST);
               
                final JTextArea textArea = new JTextArea(50,50);
                JScrollPane scroll = new JScrollPane(textArea);
                textArea.setEditable(false);
                textArea.setText("     Results");
                add(scroll, BorderLayout.CENTER);
               
                pack();
                setVisible(true);
        }
       
        /**
         * helper method to reduce redundancy.
         * @param field field to be added
         * @return jpanel containing the field
         */
        private JPanel createField(JTextField field) {
                final JPanel panel = new JPanel();
                field.setEditable(true);
                panel.add(field);
                return panel;
        }
       
        /**
         * resets ranges in southBar
         */
        private void resetRanges() {
                minPrice.setText("Minimum Price");
                minThread.setText("Minimum Threads");
                maxPrice.setText("Maximum Price");
                maxThread.setText("Maximum Threads");
        }
       
        /**
         * sets up side bar with search criteria.
         * @return JtoolBar
         */
        private JToolBar sideBar() {
                final JToolBar sidebar = new JToolBar();
                sidebar.setFloatable(false);
               
                final JPanel majorPanel = new JPanel(new BorderLayout());

                final JPanel sidePanel = new JPanel(new GridLayout(2,1));
                sidePanel.add(designBar());
                final JToolBar southbar = southBar();
                sidePanel.add(southbar);
                majorPanel.add(sidePanel);
               
                String[] searchStrings = {"Search by Design", "Search by Thread", "Search by Needles", "Find Threads for design"};
                final JComboBox searchBox = new JComboBox(searchStrings);
                searchBox.setSelectedIndex(0);
               
                // action listener for changing search type
                searchBox.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent e) {
                                String search = (String)((JComboBox)e.getSource()).getSelectedItem();
               
                                Component[] comp = sidePanel.getComponents();
                                sidePanel.remove(comp[0]);
                                sidePanel.remove(southbar);
                               
                                switch(search) {
                                case "Search by Design":
                                       
                                        sidePanel.add(designBar());
                                        break;
                                case "Search by Thread":
                                        sidePanel.add(threadBar());
                                        break;
                                case "Search by Needles":
                                        sidePanel.add(needleBar());
                                        break;
                                case "Find Threads for design":
                                        sidePanel.add(threadSearchBar());
                                        break;
                                }
                               
                                sidePanel.add(southbar);
                                revalidate();
                        }
                       
                       
                });
                majorPanel.add(searchBox, BorderLayout.NORTH);
               
                sidebar.add(majorPanel);
                return sidebar;
        }
       
        /**
         * search tool bar for search by design.
         * @return JToolBar.
         */
        private JToolBar designBar() {
                final JToolBar toolbar = new JToolBar();
                toolbar.setFloatable(false);
                toolbar.setOrientation(SwingConstants.VERTICAL);
               
                final JPanel majorPanel = new JPanel();
                majorPanel.setLayout(new GridLayout(10,1));
               
               
                final JTextField design = new JTextField("Enter Design", FIELD_SIZE);
                majorPanel.add(createField(design));
               
               
                final JTextField genre = new JTextField("Enter Genre", FIELD_SIZE);
                majorPanel.add(createField(genre));
               
               
                final JPanel machinePanel = new JPanel();
               

               
                //do stuff
                //Gets the make and models of all machines in database
                String[] machineStrings = DB.machineList("select make, model from machines");
        //      String[] machineStrings = {"machine 1", "machine 2", "machine 3"};
                final JComboBox machineBox = new JComboBox(machineStrings);
                machineBox.setSelectedIndex(0);
                machineBox.setEditable(false);
                machinePanel.add(machineBox);
                majorPanel.add(machinePanel);
               
                final JButton searchButton = new JButton("Search");
                searchButton.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                                //do stuff
                        }
                       
                });
                majorPanel.add(searchButton);
               
                final JButton resetButton = new JButton("Reset");
                resetButton.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                                design.setText("Enter Design");
                                genre.setText("Enter Genre");
                                machineBox.setSelectedIndex(0);
                               
                                resetRanges();
                        }
                       
                });
                majorPanel.add(resetButton);
               
                toolbar.add(majorPanel);
                return toolbar;
        }
       
        /**
         * search for searching by thread.
         * @return JToolBar.
         */
        private JToolBar threadBar() {
                final JToolBar toolbar = new JToolBar();
                toolbar.setFloatable(false);
                final JPanel majorPanel = new JPanel();
                majorPanel.setLayout(new GridLayout(10,1));
               
                final JTextField color = new JTextField("Enter Color", FIELD_SIZE);
                majorPanel.add(createField(color));
               
                final JTextField weight = new JTextField("Enter Weight", FIELD_SIZE);
                majorPanel.add(createField(weight));
               
                final JTextField type = new JTextField("Enter Type", FIELD_SIZE);
                majorPanel.add(createField(type));
               
                final JButton searchButton = new JButton("Search");
                searchButton.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                                //do stuff
                        }
                       
                });
                majorPanel.add(searchButton);
               
                final JButton resetButton = new JButton("Reset");
                resetButton.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                                color.setText("Enter Color");
                                weight.setText("Enter Weight");
                                type.setText("Enter Type");
                               
                                resetRanges();
                               
                        }
                       
                });
                majorPanel.add(resetButton);
               
                toolbar.add(majorPanel);
                return toolbar;
        }

        /**
         *
         * @return toolbar used for searching by needle.
         */
        private JToolBar needleBar() {
                final JToolBar toolbar = new JToolBar();
                toolbar.setFloatable(false);
                final JPanel majorPanel = new JPanel();
                majorPanel.setLayout(new GridLayout(10,1));
               
                final JTextField size = new JTextField("Enter Size", FIELD_SIZE);
                majorPanel.add(createField(size));
               
                final JTextField weight = new JTextField("Enter Weight", FIELD_SIZE);
                majorPanel.add(createField(weight));
               
                final JTextField type = new JTextField("Enter Type", FIELD_SIZE);
                majorPanel.add(createField(type));
               
                final JButton searchButton = new JButton("Search");
                searchButton.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                                //do stuff
                        }
                       
                });
                majorPanel.add(searchButton);
               
                final JButton resetButton = new JButton("Reset");
                resetButton.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                                size.setText("Enter Size");
                                weight.setText("Enter Weight");
                                type.setText("Enter Type");
                               
                                resetRanges();
                        }
                       
                });
                majorPanel.add(resetButton);
               
                toolbar.add(majorPanel);
                return toolbar;
        }
       
        /**
         *
         * @return toolbar used for searching by design number and brand.
         */
        private JToolBar threadSearchBar() {
                final JToolBar toolbar = new JToolBar();
                toolbar.setFloatable(false);
                final JPanel majorPanel = new JPanel();
                majorPanel.setLayout(new GridLayout(10,1));
               
                final JTextField design = new JTextField("Enter Design Number", FIELD_SIZE);
                majorPanel.add(createField(design));
               
               
                String[] designBrandStrings = DB.designBrandList("select distinct DesignBrand from designs");
                final JComboBox designBrandBox = new JComboBox(designBrandStrings);
                designBrandBox.setSelectedIndex(1);
                designBrandBox.setEditable(false);
                majorPanel.add(designBrandBox);
                //final JTextField brand = new JTextField("Enter Brand", FIELD_SIZE);
                //majorPanel.add(createField(brand));
               
                final JButton searchButton = new JButton("Search");
                searchButton.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                                //do stuff
                        }
                       
                });
                majorPanel.add(searchButton);
               
                final JButton resetButton = new JButton("Reset");
                resetButton.addActionListener(new ActionListener() {

                        @Override
                        public void actionPerformed(ActionEvent arg0) {
                                //brand.setText("Enter Brand");
                                design.setText("Enter Design Number");
                                designBrandBox.setSelectedIndex(1);
                               
                                resetRanges();
                        }
                       
                });
                majorPanel.add(resetButton);
               
                toolbar.add(majorPanel);
                return toolbar;
        }
       
        /**
         * @return toolbar containing price range and thread range.
         */
        private JToolBar southBar() {
                final JToolBar toolbar = new JToolBar();
                toolbar.setFloatable(false);
                final JPanel majorPanel = new JPanel(new GridLayout(5,1));
               
                // min max price range
                minPrice = new JTextField("Minimum Price", FIELD_SIZE);
                maxPrice = new JTextField("Maximum Price", FIELD_SIZE);
                majorPanel.add(createField(minPrice));
                majorPanel.add(createField(maxPrice));
               
                // min max thread range
                minThread = new JTextField("Minimum Threads", FIELD_SIZE);
                maxThread = new JTextField("Maximum Threads", FIELD_SIZE);
                majorPanel.add(createField(minThread));
                majorPanel.add(createField(maxThread));
               
               
               
                toolbar.add(majorPanel);
                return toolbar;
        }
}
